package com.meihua.mes2ehr.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meihua.mes2ehr.constants.FSConstants;
import com.meihua.mes2ehr.entity.FsMesSheet;
import com.meihua.mes2ehr.entity.FsMesSheetResult;
import com.meihua.mes2ehr.entity.FsTableResult;
import com.meihua.mes2ehr.entity.FsTokenDTO;
import com.meihua.mes2ehr.service.FsMesSheetService;
import com.meihua.mes2ehr.mapper.FsMesSheetMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author maheqiang
* @description 针对表【fs_mes_sheet】的数据库操作Service实现
* @createDate 2024-04-20 16:29:36
*/
@Service
public class FsMesSheetServiceImpl extends ServiceImpl<FsMesSheetMapper, FsMesSheet>
    implements FsMesSheetService{
    @Override
    public void getFsMesSheet() {

//        获取飞书token
        String res = HttpUtil.post(FSConstants.FS_API_URL, (String) null);
        String tenantAccessToken = JSON.parseObject(res, FsTokenDTO.class).getTenant_access_token();
//        获取表格信息

        String result = HttpUtil.createGet(FSConstants.FS_TABLE_API_URL)
                .header("Authorization", "Bearer " + tenantAccessToken)
                .execute()
                .body();
        String objToken = JSON.parseObject(result, FsTableResult.class).getData().getNode().getObjToken();
        String sheetMessageList = HttpUtil.createGet("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" + objToken + "/metainfo")
                .header("Authorization", "Bearer " + tenantAccessToken)
                .execute()
                .body();
        List<FsMesSheetResult.DataDTO.SheetsDTO> sheets = JSON.parseObject(sheetMessageList, FsMesSheetResult.class).getData().getSheets();
        ArrayList<FsMesSheet> fsMesSheets = new ArrayList<>();
        for (FsMesSheetResult.DataDTO.SheetsDTO s :
                sheets) {
            FsMesSheet fsMesSheet = new FsMesSheet();
            fsMesSheet.setSheetId(s.getSheetId());
            fsMesSheet.setTitle(s.getTitle());
            fsMesSheet.setRowCount(s.getRowCount());
            fsMesSheet.setColumnCount(s.getColumnCount());
            fsMesSheets.add(fsMesSheet);
        }
        this.saveBatch(fsMesSheets);
    }


}




